package org.wmine.eventhandlers;

import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.wmine.core.DatabaseManager;

/**
 * TODO :
 * 	1) Add one more text box to the initial screen which will ask for the database driver from the user.
 * 	   Dont add text box ,add combo box and provide with the list of JDBC drivers for ODBC,MySql etc..
 *
 */
public class ImportDatabaseEventHandler extends JDialog implements ActionListener
{
	JLabel lblConStr,lblUsername,lblPass,lblFilename;
	JButton btnConnect,btnTest,btnCancel;
	JTextField txtConStr,txtUsername,txtPass,txtFilename;
	
	private DatabaseManager databaseManager;
	
	
	public ImportDatabaseEventHandler(Frame ownerFrame) {
		super(ownerFrame,true);

		lblConStr=new JLabel("Connection String :");
		lblUsername=new JLabel("Username :");
		lblPass=new JLabel("Password :");
		//lblFilename=new JLabel("Filename :");
		
		lblConStr.setBounds(50,20,150,25);
		lblUsername.setBounds(50,90,150,25);
		lblPass.setBounds(50,160,150,25);
		//lblFilename.setBounds(50,230,150,25);
		
		txtConStr=new JTextField();
		txtUsername=new JTextField();
		txtPass=new JTextField();
		//txtFilename=new JTextField();
		
		txtConStr.setBounds(50,55,250,25);
		txtUsername.setBounds(50,125,250,25);
		txtPass.setBounds(50,195,250,25);
		//txtFilename.setBounds(50,265,250,25);
		
		btnConnect=new JButton("Connect");
		btnTest=new JButton("Test Connection");
		btnCancel=new JButton("Cancel");
		
		btnConnect.setBounds(100,315,150,25);
		btnTest.setBounds(100,350,150,25);
		//btnTest.
		btnCancel.setBounds(100,385,150,25);
		
		btnConnect.addActionListener(this);
		btnTest.addActionListener(this);
		btnCancel.addActionListener(this);
		
		
		Container c= getContentPane();
		setLayout(null);
		setBounds(300,150,350,500);
		setTitle("Import Database");
		
		c.add(lblConStr);
		c.add(txtConStr);
		c.add(lblUsername);
		c.add(txtUsername);
		c.add(lblPass);
		c.add(txtPass);
		//c.add(lblFilename);
		//c.add(txtFilename);
		c.add(btnConnect);
		c.add(btnTest);
		c.add(btnCancel);
		
		
		setVisible(true);
		
	}
	
	protected boolean connecteToDatabase(String dbName,String userName,String password) {
		DatabaseManager.setDatabaseData(dbName,userName,password);
		databaseManager = DatabaseManager.getInstance();
		return databaseManager.isConnected();
	}
	
	public static void main(String args[])
	{
		ImportDatabaseEventHandler gd = new ImportDatabaseEventHandler(null);
		gd.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		JButton but=(JButton)ae.getSource();
		
		String strConStr=txtConStr.getText();
		String strUsername=txtUsername.getText();
		String strPass=txtPass.getText();
			
		if(but==btnCancel)
		{
			JOptionPane.showMessageDialog(null,"Cancel");
			System.exit(0);
		}
		
		else if(but==btnTest)
		{
			boolean flag = connecteToDatabase(strConStr,strUsername,strPass);

			// if connection ok... display another screen			
			if(flag == true) {
				JOptionPane.showMessageDialog(null,"Test OK");
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Test Failed!! Error in connection");				
			}
		}
		
		else if(but==btnConnect)
		{
			boolean flag = connecteToDatabase(strConStr,strUsername,strPass);
		
			// if connection ok... display another screen			
			if(flag==true)
			{
				//JOptionPane.showMessageDialog(null,"Test OK");
				this.dispose();
				TableSelectionDialogBox dt=new TableSelectionDialogBox();
				dt.setDefaultCloseOperation(EXIT_ON_CLOSE);
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Test Failed!! Error in connection");				
			}
			
		}
	}
}